home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
batchut
/
ask_bat.zip
/
ASK_BAT.C
next >
Wrap
C/C++ Source or Header
|
1989-02-17
|
15KB
|
334 lines
/*********************/
/* Program ASK_BAT.C */
/*********************/
/* Copyright 1989 Floyd D. Pretz Sr.
* 5834 Spaulding St.
* Omaha, NE 68104
*
* Purpose: To parse Console input within a batch file and return with
* with an appropriate DOS ERRORLEVEL, and/or echo text to the
* console with cursor control.
*
* Input: DOS command line parameters.
*
* Output: Modified ERRORLEVEL for batch testing.
*
* Description: ask_bat was written with the intention of providing
* a method to pause for a few seconds within a batch
* file (especially autoexec) to get an option from
* the operator that would determine the next action
* that the batch file would perform. If the operator
* was unavailable, some default option could be assumed
* then batch processing continued.
*
* Use: ask_bat [ 'string' errlev ... ] [ /T:nnn ] [ /H ] [ /F ] [ /P prompt ]
*
* string - any character string to match from the keyboard.
* errlev - the ERRORLEVEL to exit with if preceding string selected.
* /T:nnn - maximum time to wait for keyboard activity.
* /H - hot key switch (On). attempt to match on one keystroke.
* /F - force match switch (On). 'string' must be matched.
* /P - prompt text follows.
* prompt - text to display prior to keyboard parsing.
*
* Notes: - string-errlev pairs, if any, must occur first in the
* parameter list.
* - parameters are space delimited.
* - switches may occur in any order following the string-errlev
* pairs.
* - prompt or string parameters must be enclosed in double
* quotes (") if either contains a space character.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define false 0
#define true 1
void main(int argc, char *argv[])
{
clock_t start, end, elapsed; /* define timer variabls */
char response[129]; /* storage for keystroks */
int timed, hotkey, forced; /* logical flags */
int i, indx, lastpair; /* miscellaneous */
int maxtime, errlev, keys; /* miscellaneous */
void ask_hlp(); /* define usage routine */
void ask_hlp_big(); /* define help routine */
/*
*
* Main Program Body
*
*/
/*
* Check for "?" parameter or absence of
* parameters and give appropriate help message.
*/
if (argc==1) { ask_hlp(); /* if no command args */
i=getch(); /* print usage, get a */
exit(0); } /* keystroke & exit */
if (!stricmp("?",argv[1])) /* if ? command arg */
{ ask_hlp_big(); /* print help & exit */
exit(0); }
/*
* Initialize local variables
*/
response[0] = 0;
start = clock(); /* get initial time stamp */
timed = hotkey = forced = false; /* default options false */
keys = 0; /* Keyboard buffer index */
errlev=-1; /* -1 indicates no match */
lastpair=argc; /* last string-errlev */
/*
* Process the command line by looping
* through the argument list looking for
* switches and setting logic flags.
*/
for (indx = 1; indx <= argc-1; indx++)
{
if (!strnicmp("/ ",argv[indx],1)) { /* switch parameter? */
if (lastpair==argc) lastpair=indx-1; /* set lastpair index */
if (!stricmp("/H",argv[indx])) /* Hot Key ? */
hotkey=true;
else if (!stricmp("/F",argv[indx])) /* Forced ? */
forced=true;
else if (!strnicmp("/T:",argv[indx],3)) /* Timed ? */
{ timed=true;
maxtime=atoi(argv[indx]+3); }
else if (!stricmp("/P",argv[indx])) /* Prompt ? */
{ if (argc >= indx)
printf(argv[++indx]); }
else
printf("Invalid switch '%s'\n",argv[indx]); /* ?????? ? */
}
}
/*
* loop indefinitely until time exceeded while gathering keystrokes.
* continue looping until parameter list satisfied (errlev != -1).
*/
do {
if (!kbhit()) { /* Keystroke waiting? */
if (timed) { /* No keyboard activity */
end=clock(); /* get & check time */
elapsed=(end-start)/13; /* CLK_TCK not used */
if (elapsed > maxtime) { /* Timeout! */
errlev=0;
if (lastpair > 0) { /* assume match on 1st */
printf (argv[1]); /* string-errlev pair */
errlev=atoi(argv[2]);
}
}
}
}
/*
* Key Hit! - Process non-ascii as a delete key.
* - Trap Hot keys
* - Buffer other keys
* - & reset clock
*/
else {
i=getch();
if (!i || i==8) { /* if null/delete then */
if (!i) i=getch(); /* process as delete */
if (keys) {
--keys; /* backup string end */
response[keys]=0;
printf("\b \b");
}
}
else {
if (i != 13) { /* Buffer keystroke & */
response[keys]=i; /* display character */
if (!hotkey) { /* if not hot key mode */
keys++;
response[keys]=0; /* string terminator */
putchar(i);
}
}
/*
* Hotkey! or <RETURN> key!
*/
if (hotkey||(i==13)) {
if (lastpair==0) errlev=0;
/*
* Loop through string-errlev pairs looking for a match
*/
for (indx = 1; indx < lastpair; indx=indx+2) {
/*
* Check hotkey
*/
if (hotkey && !strnicmp(response,argv[indx],1)) {
errlev=atoi(argv[indx+1]);
printf(argv[indx]);
}
/*
* Check non-hotkey strings
*/
else if (!stricmp(response,argv[indx]))
errlev=atoi(argv[indx+1]);
}
/*
* Process a simple <RETURN> as a default
*/
if (!forced && errlev==-1) {
if (i==13 && keys==0) {
printf(argv[1]);
errlev=atoi(argv[2]);
}
}
/*
* Bell (Beep) if no match or miss match
*/
if (errlev==-1) printf("\a");
}
}
start=clock(); }
/*
* End of keyboard processing loop
*/
} while (errlev < 0 );
printf("\n");
exit(errlev);
}
/* */
/* subroutine ask_hlp */
/*